<script setup>
import { ref } from "vue"

const title = ref("发现")
const currentTab = ref('theme')

const themeList = ref([
  {
    id: 1,
    title: "# 骑行日记 #",
    subtitle: "VLOG今日骑行日记、分享你的骑行",
    image: "/static/images/snow.jpg",
    type: "VLOG"
  },
  {
    id: 2,
    title: "# 这个冬季 # 一起去旅行",
    subtitle: "用视频记录「这个冬季」",
    image: "/static/images/snow.jpg"
  },
  {
    id: 3,
    title: "#2022年初雪#",
    subtitle: "2022年的初雪！你所在的城市下雪了吗？",
    image: "/static/images/snow.jpg"
  }
])

const roomList = ref([
  {
    id: 1,
    title: '小猪的美贝秀房',
    image: '/static/images/snow.jpg',
    users: ['/static/images/avatar.jpg', '/static/images/avatar.jpg', '/static/images/avatar.jpg'],
    onlineCount: 68
  },
  {
    id: 2,
    title: '哈利波特七部曲房',
    image: '/static/images/snow.jpg',
    users: ['/static/images/avatar.jpg', '/static/images/avatar.jpg', '/static/images/avatar.jpg'],
    onlineCount: 68
  },
  {
    id: 3,
    title: '精致姐姐屋',
    image: '/static/images/snow.jpg',
    users: ['/static/images/avatar.jpg', '/static/images/avatar.jpg', '/static/images/avatar.jpg'],
    onlineCount: 58
  },
  {
    id: 4,
    title: '波点美食',
    image: '/static/images/snow.jpg',
    users: ['/static/images/avatar.jpg', '/static/images/avatar.jpg', '/static/images/avatar.jpg'],
    onlineCount: 58
  }
])

const switchTab = (tab) => {
  currentTab.value = tab
}

const joinRoom = (room) => {
  console.log('加入房间:', room.title)
}
</script>

<template>
  <div class="discover-page">
    <custom-nav-bar :title="title" />
    
    <!-- 搜索框 -->
    <view class="search-box">
      <input type="text" placeholder="搜索用户、内容" placeholder-class="placeholder" />
    </view>

    <!-- 分类标签 -->
    <view class="category-tabs">
      <text 
        class="tab-item" 
        :class="{ active: currentTab === 'theme' }"
        @tap="switchTab('theme')"
      >主题</text>
      <text 
        class="tab-item" 
        :class="{ active: currentTab === 'room' }"
        @tap="switchTab('room')"
      >房间</text>
    </view>

    <!-- 主题列表 -->
    <view v-if="currentTab === 'theme'" class="theme-list">
      <view 
        class="theme-card" 
        v-for="theme in themeList" 
        :key="theme.id"
        @tap="handleThemeClick(theme)"
      >
        <image class="theme-image" :src="theme.image" mode="aspectFill" />
        <view class="theme-info">
          <view class="theme-content">
            <text class="theme-title">{{theme.title}}</text>
            <text class="theme-subtitle">{{theme.subtitle}}</text>
          </view>
          <text v-if="theme.type" class="theme-type">{{theme.type}}</text>
        </view>
      </view>
    </view>

    <!-- 房间列表 -->
    <view v-if="currentTab === 'room'" class="room-list">
      <view class="room-grid">
        <view class="room-card" v-for="room in roomList" :key="room.id">
          <image class="room-image" :src="room.image" mode="aspectFill" />
          <view class="room-info">
            <text class="room-title">{{room.title}}</text>
            <view class="room-users">
              <view class="user-avatars">
                <image 
                  v-for="(avatar, index) in room.users" 
                  :key="index" 
                  :src="avatar" 
                  class="user-avatar"
                />
              </view>
              <text class="online-count">{{room.onlineCount}}人在线</text>
            </view>
            <button class="join-btn" @tap="joinRoom(room)">加入房间</button>
          </view>
        </view>
      </view>
    </view>
  </div>
</template>

<style lang="scss" scoped>
.discover-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.search-box {
  margin: 20rpx;
  padding: 20rpx;
  background: #f0f0f0;
  border-radius: 40rpx;

  input {
    font-size: 28rpx;
    color: #333;
    
    &::placeholder {
      color: #999;
    }
  }
}

.category-tabs {
  display: flex;
  padding: 0 40rpx;
  margin-bottom: 20rpx;
  
  .tab-item {
    position: relative;
    padding: 20rpx 40rpx;
    font-size: 32rpx;
    color: #666;
    
    &.active {
      color: #333;
      font-weight: bold;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 40rpx;
        height: 4rpx;
        background-color: #007AFF;
        border-radius: 2rpx;
      }
    }
  }
}

.theme-list {
  padding: 0 20rpx;
}

.theme-card {
  position: relative;
  margin-bottom: 20rpx;
  border-radius: 16rpx;
  overflow: hidden;
  background: #fff;
  
  .theme-image {
    width: 100%;
    height: 300rpx;
    display: block;
  }
  
  .theme-info {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.3);
  }
  
  .theme-content {
    text-align: center;
    padding: 20rpx;
  }
  
  .theme-title {
    display: block;
    font-size: 36rpx;
    font-weight: bold;
    margin-bottom: 8rpx;
    color: #fff;
  }
  
  .theme-subtitle {
    display: block;
    font-size: 24rpx;
    color: #fff;
    opacity: 0.8;
  }
  
  .theme-type {
    position: absolute;
    top: 20rpx;
    left: 20rpx;
    padding: 4rpx 12rpx;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 8rpx;
    font-size: 24rpx;
    color: #fff;
  }
}

.room-list {
  padding: 20rpx;
}

.room-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.room-card {
  position: relative;
  border-radius: 16rpx;
  background: #fff;
  overflow: hidden;
}

.room-image {
  width: 100%;
  height: 300rpx;
  display: block;
}

.room-info {
  padding: 16rpx;
}

.room-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 12rpx;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.room-users {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.user-avatars {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  border: 2rpx solid #fff;
  margin-right: -12rpx;
  &:last-child {
    margin-right: 0;
  }
}

.online-count {
  font-size: 22rpx;
  color: #666;
}

.join-btn {
  width: 100%;
  height: 64rpx;
  line-height: 64rpx;
  text-align: center;
  font-size: 28rpx;
  color: #fff;
  border: none;
  border-radius: 32rpx;
  background: linear-gradient(to right, #5B8EFF, #3D73FF);
  box-shadow: 0 4rpx 8rpx rgba(61, 115, 255, 0.2);
  
  &::after {
    border: none;
  }
  
  &:active {
    opacity: 0.9;
  }
}
</style>
